package com.avocado.configuration.controller;

import com.avocado.commons.model.http.BaseResponse;
import com.avocado.commons.utils.JsonUtils;
import com.avocado.configuration.dto.IdGeneratorAddDto;
import com.avocado.configuration.dto.IdGeneratorEditDto;
import com.avocado.configuration.dto.IdGeneratorQueryDto;
import com.avocado.configuration.service.IdGeneratorService;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/idGenerator/*")
public class IdGeneratorController {

	private final IdGeneratorService idGeneratorService;

    public IdGeneratorController(IdGeneratorService idGeneratorService) {
        this.idGeneratorService = idGeneratorService;
    }


         @RequestMapping("/getOne")
	     public @ResponseBody BaseResponse getOne(Long id) {
	     	 return BaseResponse.builder().status(1).data(idGeneratorService.getOne(id)).build();
	     }
		
		 @RequestMapping("/add")
	     public @ResponseBody BaseResponse add(@RequestBody @Validated IdGeneratorAddDto dto) throws Exception{

		     long id=idGeneratorService.add(dto);

			 return BaseResponse.builder().status(1).message("操作成功!").data(id).build();
	     }
	    
		 @RequestMapping("/update")
	     public @ResponseBody BaseResponse update(@RequestBody @Validated IdGeneratorEditDto dto) throws Exception{


			 idGeneratorService.update(dto);

			 return BaseResponse.builder().status(1).message("操作成功!").build();
	     }
		
		 @RequestMapping("/findByPage")
	     public @ResponseBody BaseResponse findByPage(@PageableDefault Pageable pageable, IdGeneratorQueryDto dto) {

	      	 return BaseResponse.builder().status(1).data(idGeneratorService.findByPage(pageable, dto)).build();
	     }
	    
		 @RequestMapping("/findAll")
	     public @ResponseBody BaseResponse findAll(IdGeneratorQueryDto dto) {

			 return BaseResponse.builder().status(1).data(idGeneratorService.findAll(dto)).build();
	     }
		
		 @RequestMapping("/deleteById")
	     public @ResponseBody BaseResponse deleteById(Long id) {

			 int status=idGeneratorService.deleteById(id)==Boolean.TRUE?1:0;

			 return BaseResponse.builder().status(status).message("操作成功!").build();
	     }
		
		

}
